var nDots = 5;
//if (document.all&&window.print)
// document.body.style.cssText="overflow-x:hidden;overflow-y:scroll"
var Xpos = 0;
var Ypos = 0;

// Фиксируется временной промежуток
var DELTAT = .02;
// Размеры
var SEGLEN = 10;
var SPRINGK = 10;
// Фиксируются физические константы

var MASS = 1;
var GRAVITY = 50;
var RESISTANCE = 10;
// Условия останова

var STOPVEL = 0.1;
var STOPACC = 0.1;
var DOTSIZE = 30;
// Дополнительные условия

var BOUNCE = 0.75;

var isNetscape = navigator.appName=="Netscape";
var isIE = navigator.appName=="Microsoft Internet Explorer";

var followmouse = true;

var dots = new Array();
var dotsX = new Array();
var dotsY = new Array();
var dotsdx = new Array();
var dotsdy = new Array();
//initLapa();

function initLapa()
{
    var i = 0;

    for (i = 0; i < nDots; i++) {
        /*$('document').app*/
        dotsX[i] = 0;
        dotsY[i] = 0;
        dotsdx[i] = 0;
        dotsdy[i] = 0;
    }

    // set their positions
    for (i = 0; i < nDots; i++) {

        document.getElementById("dot"+i).style.left = dotsX[i];
        document.getElementById("dot"+i).style.top = dotsY[i];
    }


    /*  if (isNetscape) {

     startanimate();
     } else {*/

    setTimeout("startanimate()", 2000);
    //  }
}

function startanimate() {
    setInterval("animate()", 20);

}

function setInitPositions(dots)
{
    // initialize dot positions to be on top 
    // of the bullets in the <ul>
    var startloc = document.all.tags("LI");
    var i = 0;
    for (i = 0; i < startloc.length && i < (nDots - 1); i++) {
        dotsX[i+1] = startloc[i].offsetLeft;
        //startloc[i].offsetParent.offsetLeft - DOTSIZE;
        dotsY[i+1] = startloc[i].offsetTop +
        startloc[i].offsetParent.offsetTop + 2*DOTSIZE;
    }
    // put 0th dot above 1st (it is hidden)
    dotsX[0] = dotsX[1];


    dotsY[0] = dotsY[1] - SEGLEN;
}

// just save mouse position for animate() to use
function MoveHandler(e)
{
    // document.captureEvents(Event.MOUSEMOVE);
    Xpos = event.pageX;
    Ypos = event.pageY;


    return true;
}

// just save mouse position for animate() to use
function MoveHandlerIE() {
    Xpos = window.event.x + document.body.scrollLeft;
    Ypos = window.event.y + document.body.scrollTop;

}


/* if (isNetscape) {
 document.captureEvents(Event.MOUSEMOVE);
 document.onMouseMove = MoveHandler;
 } else {*/
if (isIE) {
    document.onmousemove = MoveHandlerIE;
} else {
    document.addEventListener ("mousemove", getCoord,false);
}
//}

function getCoord( event ) {

    Xpos = event.pageX;
    Ypos = event.pageY;

}

function vec(X, Y)
{
    this.X = X;
    this.Y = Y;
}

// adds force in X and Y to spring for dot[i] on dot[j]
function springForce(i, j, spring)
{
    var dx = (dotsX[i] - dotsX[j]);
    var dy = (dotsY[i] - dotsY[j]);
    var len = Math.sqrt(dx*dx + dy*dy);
    if (len > SEGLEN) {
        var springF = SPRINGK * (len - SEGLEN);
        spring.X += (dx / len) * springF;
        spring.Y += (dy / len) * springF;
    }
}


function animate() {
    // dots[0] follows the mouse,
    // though no dot is drawn there
    var start = 0;
    if (followmouse) {
        dotsX[0] = Xpos;
        dotsY[0] = Ypos;
        start = 1;
    }

    for (i = start ; i < nDots; i++ ) {

        var spring = new vec(0, 0);
        if (i > 0) {
            springForce(i-1, i, spring);
        }
        if (i < (nDots - 1)) {
            springForce(i+1, i, spring);
        }

        // air resisitance/friction
        var resist = new vec(-dotsdx[i] * RESISTANCE,
            -dotsdy[i] * RESISTANCE);

        // compute new accel, including gravity
        var accel = new vec((spring.X + resist.X)/ MASS,
            (spring.Y + resist.Y)/ MASS + GRAVITY);

        // compute new velocity
        dotsdx[i] += (DELTAT * accel.X);
        dotsdy[i] += (DELTAT * accel.Y);

        // stop dead so it doesn't jitter when nearly still
        if (Math.abs(dotsdx[i]) < STOPVEL &&
            Math.abs(dotsdy[i]) < STOPVEL &&
            Math.abs(accel.X) < STOPACC &&
            Math.abs(accel.Y) < STOPACC) {
            dotsdx[i] = 0;
            dotsdy[i] = 0;
        }

        // move to new position
        dotsX[i] += dotsdx[i];
        dotsY[i] += dotsdy[i];

        // get size of window
        var height, width;


        // bounce of 3 walls (leave ceiling open)
        if (dotsY[i] >=  height - DOTSIZE - 1) {
            if (dotsdy[i] > 0) {
                dotsdy[i] = BOUNCE * -dotsdy[i];
            }
            dotsY[i] = height - DOTSIZE - 1;
        }
        if (dotsX[i] >= width - DOTSIZE) {
            if (dotsdx[i] > 0) {
                dotsdx[i] = BOUNCE * -dotsdx[i];
            }
            dotsX[i] = width - DOTSIZE - 1;
        }
        if (dotsX[i] < 0) {
            if (dotsdx[i] < 0) {
                dotsdx[i] = BOUNCE * -dotsdx[i];
            }
            dotsX[i] = 0;
        }

        // move img to new position
        possx = dotsX[i] - 15;
        possy = dotsY[i];

        if (xscr-100 < possx)  possx = xscr-100;

        getScrollXY();
        if (document.implementation && document.implementation.createDocument){

            if (yscr+ scrOfY -55 < possy)  possy = scrOfY + yscr -55;



        } else {
            if (yscr-55 < possy)  possy = yscr-55;
        }


        if (isNetscape) {if (yscr+ scrOfY -55 < possy)  possy = scrOfY + yscr -55; }

        document.getElementById("dot"+i).style.left = possx + 'px';


        if (document.implementation && document.implementation.createDocument){
            document.getElementById("dot"+i).style.top = possy + 'px';	}
        else {document.getElementById("dot"+i).style.top = possy + scrOfY + 'px';}


    }
}


window.onresize = dblock;
//window.onscroll = getScrollXY;
dblock();
//getScrollXY();

var xscr, yscr;
var scrOfX = 0, scrOfY = 0, ddd=5;
function dblock() {


    getScrollXY();


    if (self.innerHeight) {
        xscr = self.innerWidth;
        yscr = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) {
        xscr = document.documentElement.clientWidth;
        yscr = document.documentElement.clientHeight;
    } else if (document.body) {
        xscr = document.body.clientWidth;
        yscr = document.body.clientHeight;
    }
}

function getScrollXY() {
    if (document.documentElement && document.documentElement.clientHeight) {
        if( typeof( window.pageYOffset ) == 'number') {
            //Netscape compliant
            scrOfY = window.pageYOffset;
            scrOfX = window.pageXOffset;

        } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
            //DOM compliant
            scrOfY = document.body.scrollTop;
            scrOfX = document.body.scrollLeft;
        } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
            //IE6 standards compliant mode
            scrOfY = document.documentElement.scrollTop;
            scrOfX = document.documentElement.scrollLeft;
        }
        //	if(scrOfX > xscr) scrOfX = xscr;
        //	if(scrOfY > yscr) scrOfY = yscr;
        return [ scrOfX, scrOfY ];
    }
}
// end code hiding -->